$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'gen/genTable/list',
        datatype: "json",
        colModel: [			
			{ label: '表名', name: 'name', index: 'name', width: 80 },
			{ label: '说明', name: 'comments', index: 'comments', width: 80 },
			{ label: '类名', name: 'className', index: 'className', width: 80 },
			{ label: '父类', name: 'parentTable', index: 'parentTable', width: 80 }
		],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
        q:{
            name: null
        },
		showList: true,
        showChildList: false,
		title: null,
		genTable: {},
        tableList: {},
        columnList: {},
        queryTypeList: {},
        javaTypeList: {},
        showTypeList: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "选择表名";
			vm.genTable = {};
            $.get(baseURL + "gen/genTable/tableList", function(r){
                vm.tableList = r.tableList;
            });
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.showChildList = true;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.genTable.id == null ? "gen/genTable/save" : "gen/genTable/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.genTable),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "gen/genTable/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "gen/genTable/info/"+id, function(r){
                vm.genTable = r.genTable;
                vm.columnList = vm.genTable.columnList;
                vm.queryTypeList = r.config.queryTypeList;
                vm.javaTypeList = r.config.javaTypeList;
                vm.showTypeList = r.config.showTypeList;
            });
		},
		reload: function (event) {
			vm.showList = true;
			vm.showChildList = false;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{'name': vm.q.name},
                page:page
            }).trigger("reloadGrid");
		},
        nextStep: function (event) {
            var options = $("#tableName option:selected");
            var tableName = options.val();
            $.get(baseURL + "gen/genTable/columnList?name="+tableName, function(r){
                if(r.code == 0){
                    vm.showList = false;
                    vm.showChildList = true;
                    vm.genTable = r.genTable;
                    vm.columnList = vm.genTable.columnList;
                    vm.queryTypeList = r.config.queryTypeList;
                    vm.javaTypeList = r.config.javaTypeList;
                    vm.showTypeList = r.config.showTypeList;
                }else{
                    alert(r.msg);
                }

            });
        }
	}
});